package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.Log;
import com.hghivln.blog.mapper.LogMapper;
import com.hghivln.blog.pojo.dto.LogDto;
import com.hghivln.blog.service.LogService;
import com.hghivln.blog.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-08-29
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Autowired
    private IdWorker idWorker;

    @Override
    public void saveLog(Log log) {
        log.setLogId(idWorker.nextId());
        log.setCreatedBy("admin");
        getBaseMapper().insert(log);
    }

    @Override
    public Result<?> loadAllLog(LogDto logDto) {
        Page<Log> page = new Page<>(logDto.getPage(), logDto.getLimit());
        QueryWrapper<Log> qw = new QueryWrapper<>();
        qw.like(!StringUtils.isEmpty(logDto.getLogUrl()),"log_url", logDto.getLogUrl());
        qw.like(!StringUtils.isEmpty(logDto.getCreatedBy()),"created_by", logDto.getCreatedBy());
        qw.eq(!StringUtils.isEmpty(logDto.getLogStatus()),"log_status", logDto.getLogStatus());
        qw.ge(logDto.getStartDate()!=null,"created_date", logDto.getStartDate());
        qw.le(logDto.getEndDate()!=null,"created_date", logDto.getEndDate());
        // 查询指定的字段
        qw.select("log_id","log_url","log_status","log_ua","log_controller","log_method","log_time","log_ip","created_date","created_by");
        qw.orderByDesc("created_date");
        getBaseMapper().selectPage(page,qw);
        return new Result<>(page.getTotal(),page.getRecords());
    }
}
